﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.Core
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Sapiens.at.Core.Utils
{
    public static class StringExtensions
    {
        public static string Aggregate(this string[] array, string delimiter)
        {
            if (array == null || array.Length == 0) { return null; }
            var stringBuilder = array.Aggregate(
                new StringBuilder(),
                (sb, str) => sb.AppendFormat("{0}{1}", str, delimiter),
                sb => sb.Remove(sb.Length - 1, 1));
            return stringBuilder.ToString();
        }

        public static string[] TrimmedSplit(this string value, string separator)
        {
            if(string.IsNullOrEmpty(value)){ return new string[0]; }
            return value.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries)
                .Select(str => str.Trim())
                .Where(str => !string.IsNullOrEmpty(str))
                .ToArray();
        }

        public static bool IsEMailAddress(this string value)
        {
            return Regex.IsMatch(value, "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$");        
        }
    }
}
